<template>
  <div id="article-message">
    <ElRow :gutter="20">
      <ElCol :xs="{ span: 24 }" :md="{ span: 11 }">
        <h3>评论</h3>
        <form>
          <div class="input-lab">
            <input v-model="formdata.name" type="text" placeholder="名字" />
            &nbsp;
            <input v-model="formdata.phone" type="text" placeholder="联系" />
          </div>

          <p>
            <textarea
              v-model="formdata.content"
              name="content"
              id="content"
              rows="8"
              placeholder="内容"
            ></textarea>
          </p>
        </form>
        <button class="btn" @click="subEve">发送</button>
      </ElCol>
      <ElCol :xs="{ span: 24 }" :md="{ span: 13 }">
        <h3>评论内容</h3>
        <ul class="ul-Layout">
          <li v-for="(item, i) in list" :key="i">
            <div class="userMsg">
              <span>{{ item.name }}</span>
              <span>{{ gettime(item.updatetime) }}</span>
            </div>
            <p>{{ item.content }}</p>
          </li>
        </ul>
        <el-pagination
          layout="total,prev, pager, next"
          :total="total"
          :current-page="listQuery.page"
          @current-change="init"
          :page-size.sync="listQuery.limit"
          background
        >
        </el-pagination>
      </ElCol>
    </ElRow>
  </div>
</template>
<script>
import { addmessage, getmessage } from "@/api/Article";
import { dateStr } from "@/utils/getDateDiff";
const defFormData = {
  name: "",
  phone: "",
  content: "",
  ArticleId: "",
};
export default {
  name: "ArticleMessage",
  data() {
    return {
      formdata: Object.assign({}, defFormData),
      total: 0,
      list: [],
      listQuery: {
        page: 1,
        limit: 5,
      },
    };
  },
  created() {
    this.init();
  },
  activated() {
    this.init();
  },
  deactivated() {
    this.formdata = Object.assign({}, defFormData);
  },
  methods: {
    init(page = 1) {
      getmessage({
        page,
        limit: 5,
        _id: JSON.parse(localStorage.getItem("item"))._id,
      }).then((res) => {
        console.log("评论列表", res);
        this.total = res.data.total;
        this.list = res.data.flag;
      });
    },
    subEve() {
      if (this.formdata.name != "" && this.formdata.content != "") {
        this.formdata.ArticleId = JSON.parse(localStorage.getItem("item"))._id;
        addmessage(this.formdata).then((res) => {
          this.$message({
            message: res.msg,
            type: res.flag ? "success" : "error",
            offset: 80,
          });
          this.init();
          res.flag ? (this.formdata = Object.assign({}, defFormData)) : "";
        });
      } else {
        this.$message({
          message: "名字与内容不能为空",
          type: "error",
        });
      }
    },
    gettime(time) {
      return dateStr(time);
    },
  },
};
</script>
<style lang='scss' scoped>
#article-message {
  .input-lab {
    display: flex;
  }
  .ul-Layout {
    min-height: 220px;
    li {
      margin-bottom: 20px;

      p {
        margin: 0;
        margin-top: 5px;
        color: #333;
      }
      .userMsg {
        display: flex;
        justify-content: space-between;
        span:nth-child(1) {
          color: #333;
          font-weight: 700;
        }
        span:nth-child(2) {
          font-size: 12px;
          color: #333;
        }
      }
    }
  }
  textarea,
  input {
    width: 50%;
    outline: none;
    border: 0.01rem solid #999;
    border-radius: 4px;
    height: 30px;
    padding-left: 10px;
    transition: border 0.1s;
    font-size: 14px;
    color: #333;
    &:focus {
      border: 1px solid #ff9000;
    }
  }
  textarea {
    padding: 2%;
    height: 150px;
    line-height: 150%;
    letter-spacing: 1px;
    width: 96%;
  }
  .btn {
    width: 100px;
    height: 40px;
    border: none;
    background-color: #ff9000;
    color: #fff;
    letter-spacing: 2px;
  }
}
</style>